package com.myk.game.gobangchess.utils;

import com.myk.game.gobangchess.constants.PathCost;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class Image2Grids {
    public static void main(String[] args) {
        //将图片转换为二维数组
        String imageFilePath = PathCost.ROOT_PATH + "/tools/image/test2.png";
        BufferedImage image = getImageByPath(imageFilePath);
        //将图片压缩到指定尺寸
        image = resizeImage(image, 1000, 1000);
        //将图片转换为二维数组
        int[][] imgArr = imageToArr(image);

        // 输出原图片到指定文件
        BufferedImage newImage = drawImage(imgArr);
        String outputFilePath = PathCost.ROOT_PATH + "/tools/image/output/aaa.png";
        outputImage(newImage, outputFilePath);

        // 输出原图片到指定文件
        BufferedImage newImage2 = drawGrayImage(imgArr);
        String outputFilePath2 = PathCost.ROOT_PATH + "/tools/image/output/bbb.png";
        outputImage(newImage2, outputFilePath2);

        //绘制马赛克图像
        BufferedImage newImage3 = drawMosaicImage(imgArr);
        String outputFilePath3 = PathCost.ROOT_PATH + "/tools/image/output/ccc.png";
        outputImage(newImage3, outputFilePath3);

        //绘制黑白图像
        BufferedImage newImage4 = drawBlackWhiteImage(imgArr);
        String outputFilePath4 = PathCost.ROOT_PATH + "/tools/image/output/ddd.png";
        outputImage(newImage4, outputFilePath4);

        //绘制轮廓图像
        BufferedImage newImage5 = drawOutlinesImage(imgArr);
        String outputFilePath5 = PathCost.ROOT_PATH + "/tools/image/output/eee.png";
        outputImage(newImage5, outputFilePath5);
    }

    //根据路径获取图片
    public static BufferedImage getImageByPath(String imageFilePath) {
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File(imageFilePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }

    //将图片输出到指定文件
    public static void outputImage(BufferedImage image, String outputFilePath) {
        try {
            //创建文件夹
            File file = new File(outputFilePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            ImageIO.write(image, "png", file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //将图片转换为二维数组
    public static int[][] imageToArr(BufferedImage image) {
        int w = image.getWidth();
        int h = image.getHeight();
        //存储像素点的二维数组
        int[][] imgArr = new int[w][h];

        for(int i = 0; i < w; i++) {
            for(int j = 0; j < h; j++) {
                imgArr[i][j] = image.getRGB(i, j);
            }
        }
        return imgArr;
    }

    //将图片压缩到指定尺寸
    public static BufferedImage resizeImage(BufferedImage image, int targetWidth, int targetHeight) {
        BufferedImage newImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = newImage.createGraphics();
        gr.drawImage(image, 0, 0, targetWidth, targetHeight, null);
        gr.dispose();
        return newImage;
    }

    //原样绘制图片
    public static BufferedImage drawImage(int[][] imgArr) {
        BufferedImage image = new BufferedImage(imgArr.length, imgArr[0].length, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = image.createGraphics();
        for(int i = 0; i < imgArr.length; i++) {
            for(int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb,true);
                gr.setColor(color);
                gr.fillRect(i,j,1,1);
            }
        }
        return image;
    }

    //绘制灰度图像
    public static BufferedImage drawGrayImage(int[][] imgArr) {
        BufferedImage image = new BufferedImage(imgArr.length, imgArr[0].length, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = image.createGraphics();
        for(int i = 0; i < imgArr.length; i++) {
            for(int j = 0; j < imgArr[0].length; j++) {

                int rgb = imgArr[i][j];
                int red = (rgb >> 16) & 0xFF;
                int green = (rgb >> 8) & 0xFF;
                int blue = (rgb >> 0) & 0xFF;
                int gray = (red + green + blue) / 3;

                Color color = new Color(gray, gray, gray);
                gr.setColor(color);
                gr.drawLine(i, j, i, j);

            }
        }
        return image;
    }

    //绘制马赛克图像
    public static BufferedImage drawMosaicImage(int[][] imgArr) {
        BufferedImage image = new BufferedImage(imgArr.length, imgArr[0].length, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = image.createGraphics();
        for(int i = 0; i < imgArr.length; i+=16) {
            for(int j = 0; j < imgArr[0].length; j+=16) {
                Color c2 = new Color(imgArr[i][j]);
                //画方格子，实现方格状马赛克
                gr.setColor(c2);
                gr.fillRect(i, j, 16, 16);
            }
        }
        return image;
    }

    //绘制黑白图像
    public static BufferedImage drawBlackWhiteImage(int[][] imgArr) {
        BufferedImage image = new BufferedImage(imgArr.length, imgArr[0].length, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = image.createGraphics();
        for(int i = 0; i < imgArr.length; i++) {
            for(int j = 0; j < imgArr[0].length; j++) {

                int rgb = imgArr[i][j];
                int red = (rgb >> 16) & 0xFF;
                int green = (rgb >> 8) & 0xFF;
                int blue = (rgb >> 0) & 0xFF;
                int gray = (red + green + blue) / 3;
                if(gray > 100) {
                    gr.setColor(Color.WHITE);
                } else {

                    gr.setColor(Color.BLACK);
                }
                gr.drawLine(i, j, i, j);
            }
        }
        return image;
    }

    //绘制轮廓图像
    public static BufferedImage drawOutlinesImage(int[][] imgArr) {
        BufferedImage image = new BufferedImage(imgArr.length, imgArr[0].length, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = image.createGraphics();
        //-3防止越界
        for(int i = 0; i < imgArr.length-3; i++) {
            for(int j = 0; j < imgArr[0].length-3; j++) {

                int rgb = imgArr[i][j];
                int red = (rgb >> 16) & 0xFF;
                int green = (rgb >> 8) & 0xFF;
                int blue = (rgb >> 0) & 0xFF;
                int gray = (red + green + blue) / 3;
                // 获取三个单通道的RGB值
                int rgb2 = imgArr[i+3][j+3];
                int red2 = (rgb2 >> 16) & 0xFF;
                int green2 = (rgb2 >> 8) & 0xFF;
                int blue2 = (rgb2 >> 0) & 0xFF;
                int gray2 = (red2 + green2 + blue2) / 3;
                //边缘检测
                if(Math.abs(gray-gray2) > 5) {
                    gr.setColor(Color.WHITE);
                } else {
                    gr.setColor(Color.BLACK);
                }
                gr.drawLine(i, j, i, j);
            }
        }
        return image;
    }

    //图片转成16进制字符串的二维数组，逆界实测最大分辨率为48x48
    public static String[][] getStrArr(String imageFilePath,int row, int col) {
        String[][] strArr = new String[row][col];
        BufferedImage image = getImageByPath(imageFilePath);
        image = resizeImage(image, col, row);
        int[][] imgArr = imageToArr(image);
        //转换成十六进制字符串
        for (int i = 0; i < col; i++) {
            for (int j = 0; j < row; j++) {
                if (imgArr[j][i] != 0){
                    String hex = Integer.toHexString(imgArr[j][i]);
                    if (hex.length() == 7){
                        hex = "0" + hex;
                    }
                    strArr[i][j] = "#" + hex;
                }
            }
        }
        return strArr;
    }

    public static int[][] image2Grids(String imageFilePath, int row, int col) {
        int[][] newGrids = new int[row][col];
        BufferedImage image = getImageByPath(imageFilePath);
        image = resizeImage(image, col, row);
        int[][] imgArr = imageToArr(image);
        //转换成十六进制字符串
        for (int i = 0; i < col; i++) {
            for (int j = 0; j < row; j++) {
                int rgb = imgArr[j][i];
                //如果rgb靠近黑色，则设置为11，如果rgb靠近白色设置为12，如果rgb靠近灰色设置为17，如果rgb靠近红色设置为13，如果rgb靠近蓝色设置为14，如果rgb靠近黄色设置为15，如果rgb靠近绿色设置为16
                if(rgb < 128) {
                    newGrids[i][j] = 11;
                } else if(rgb > 128 && rgb < 192) {
                    newGrids[i][j] = 12;
                } else if(rgb > 192 && rgb < 224) {
                    newGrids[i][j] = 17;
                } else if(rgb > 224 && rgb < 256) {
                    newGrids[i][j] = 13;
                }
                if(rgb > 192) {
                    newGrids[i][j] = 14;
                }
                if(rgb > 224) {
                    newGrids[i][j] = 15;
                }
                if(rgb > 255) {
                    newGrids[i][j] = 16;
                }
            }
        }
        return newGrids;
    }
}
